package com.woniu.clientServer.service.impl;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.clientServer.dto.CustomerApplicationDto;
import com.woniu.clientServer.feign.WorkFeign;
import com.woniu.clientServer.mapper.ApprovalRecordMapper;
import com.woniu.clientServer.mapper.CustomerMapper;
import com.woniu.clientServer.pojo.ApprovalRecord;
import com.woniu.clientServer.pojo.Customer;
import com.woniu.clientServer.pojo.CustomerApplication;
import com.woniu.clientServer.service.CustomerApplicationService;
import com.woniu.clientServer.mapper.CustomerApplicationMapper;
import com.woniu.common.utils.ResponseData;
import com.woniu.common.utils.ResponseEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 404
 * @description 针对表【customer_application(客户表)】的数据库操作Service实现
 * @createDate 2024-11-27 16:12:27
 */
@Service
public class CustomerApplicationServiceImpl extends ServiceImpl<CustomerApplicationMapper, CustomerApplication>
        implements CustomerApplicationService{
    @Autowired
    private CustomerApplicationMapper customerApplicationMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private ApprovalRecordMapper recordMapper;
    @Autowired
    private WorkFeign workFeign;

    @Override
    public ResponseData<?> clientApp(CustomerApplicationDto customerApplicationDto) {
        Integer customerId = customerApplicationDto.getCustomerId();
        CustomerApplication customerApplication = customerApplicationMapper.queryById(customerApplicationDto.getCustomerId());
        Customer customer = customerMapper.queryById(customerApplicationDto.getCustomerId());
        customerApplicationDto.setStatus(0);
        customerApplicationDto.setCode(String.valueOf(IdUtil.getSnowflake(1,1).nextId()));
        customerApplicationDto.setPersonName(customerApplicationDto.getApprovalName());
        customerApplicationDto.setPersonCode(customerApplicationDto.getApprovalCode());
        if (customerId == null || (customerApplication == null && customer == null)){
            //写入申请表
            this.save(customerApplicationDto);
            //启动审批工作流
            String bpmnFile = "ProjectApplication.bpmn20.xml";
            ResponseData<String> responseData1 = workFeign.deployFlow(bpmnFile);
            ResponseData<String> responseData2 = workFeign.startProcess(responseData1.getData());
            //写入审批表
            ApprovalRecord approvalRecord = new ApprovalRecord();
            approvalRecord.setApprovalRecordCode(String.valueOf(IdUtil.getSnowflake(1,1).nextId()));
            approvalRecord.setApplicationCustomerCode(customerApplicationDto.getCode());
            approvalRecord.setApprovalName(customerApplicationDto.getApprovalName());
            approvalRecord.setApprovalCode(customerApplicationDto.getApprovalCode());
            approvalRecord.setStatus(1);
            approvalRecord.setProcessInstanceId(responseData2.getData());
            approvalRecord.setApprovalResult(1);
            approvalRecord.setCreatedBy(customerApplicationDto.getApprovalName());
            recordMapper.save(approvalRecord);
            return new ResponseData<>().ok();
        }
        return new ResponseData<>().fail(ResponseEnum.FAIL);
    }

    @Override
    public void updateStatus(CustomerApplication customerApplication) {
        lambdaUpdate().eq(CustomerApplication::getCode,customerApplication.getCode())
                .set(CustomerApplication::getStatus,customerApplication.getStatus())
                .update();
    }

    @Override
    public ResponseData<?> queryAppByCode(String code) {
        List<CustomerApplicationDto> customerApplicationDtos = recordMapper.queryAppByCode(code);
        return new ResponseData<>().ok(customerApplicationDtos);
    }
}




